﻿package com.sos.ui
{ 
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	
    public class PlayMenu extends MovieClip 
    { 
	
		public var position:String = '';
		public var playArea:PlayArea;
		public var tooltipText:TextField;
		private var activeFilter = new GlowFilter(0xd2d2d2, .75, 7, 7, 2, 2, false, false);
		
		public function PlayMenu() {
				this.visible = false;
				msgBox.visible = false;
				//trace("Menu hidden");
				hideMenuButtons();
				tooltipText.width = 410;
				tooltipText.height = 50;
				tooltipText.border = true;
				tooltipText.x = -124;
				tooltipText.y = 30;
				tooltipText.multiline = true;
				tooltipText.wordWrap = true;
				tooltipText.visible = false;
				tooltipText.background = true;
			}
		
		public function init(pos:String, playArea:PlayArea):void {
			position = pos;
			this.playArea = playArea;
			showMenuButtons();
			
			driButton.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler, false, 0, true);
			passButton.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler, false, 0, true);
			shootButton.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler, false, 0, true);
			crossButton.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler, false, 0, true);
			
			driButton.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler, false, 0, true);
			passButton.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler, false, 0, true);
			shootButton.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler, false, 0, true);
			crossButton.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler, false, 0, true);
			
		} 
		
		function rollOverHandler(e:MouseEvent) {
			tooltipText.visible = true;
			switch (e.target.name) {
				case "driButton":
					tooltipText.text = "When you successfully dribble, the active player and target player switch their positions.";
				break;
				case "passButton":
					tooltipText.text = "When you successfully pass, target player receives the ball.";
				break;
				case "shootButton":
					tooltipText.text = "When you shoot, the active player tries to dribble his opponent; if he is successfull, he will have a chance to score.";
					tooltipText.height = 70;
				break;
				case "crossButton":
					tooltipText.text = "When you cross, the active player tries to pass the ball to the other forward; if he is successfull, that forward will have a chance to score.";
					tooltipText.height = 70;
				break;
			}
		}
		
		function rollOutHandler(e:MouseEvent) {
			tooltipText.text = '';
			tooltipText.height = 50;
			tooltipText.visible = false;
		}
		
		function unsetFilters() {
			driButton.filters = [];
			passButton.filters = [];
			shootButton.filters = [];
			crossButton.filters = [];
		}
		
		function handleLeftClick(e:MouseEvent) {
			unsetFilters();
			e.target.filters = [activeFilter];
			switch (e.target.name) {
				case "driButton":
					startDribbleTargeting();
				break;
				case "passButton":
					startPassTargeting();
				break;
				case "shootButton":
					setShootAction();
				break;
				case "crossButton":
					setCrossAction();
				break;
			}
		}
		
		function setTargetingMessage(msg:String) {
			hideMenuButtons();
			msgBox.text = msg;
			msgBox.visible = true;
		}
		
		function hideMenuButtons() {
			//unsetFilters();
			driButton.visible = false;
			passButton.visible = false;
			shootButton.visible = false;
			crossButton.visible = false;
			this.removeEventListener(MouseEvent.CLICK, handleLeftClick);
		}
		
		function showMenuButtons() {
			//unsetFilters();
			if (msgBox.visible == true)
				msgBox.visible = false;
			position = playArea.fieldMc.playerList[playArea.activePlayers[playArea.clientSide]].position;
			if (playArea.activePlayers[playArea.clientSide] == playArea.ballHandler) { 
				driButton.visible = true;
				passButton.visible = true;
				
				if ((position == "f1")||(position == "f2"))
				{
					shootButton.visible = true;
					crossButton.visible = true;
				}
				this.addEventListener(MouseEvent.CLICK, handleLeftClick,false,0,true);
			}
		}
		
		function startDribbleTargeting() {
			setTargetingMessage("Select dribble destination");
			playArea.setRoundAction("dribble");
			playArea.enterTargetMode(playArea.dribbleTargets, 'roundAction');
		}
		function startPassTargeting() {
			setTargetingMessage("Select pass target");
			playArea.setRoundAction("pass");
			playArea.enterTargetMode(playArea.passTargets, 'roundAction');
		}
		
		function setShootAction() {
			playArea.setRoundAction("shoot");
		}
		
		function setCrossAction() {
			playArea.setRoundAction("cross");
		}
		
    } 
}